<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command\ocean;

use app\command\BaseWork;
use app\enum\CommonEnum;
use app\model\mysql\OeRebateMaterialModel;
use app\service\client\AccountService;
use app\service\ocean\MaterialService;
use app\service\ocean\VideoService;
use think\console\Input;
use think\console\Output;

class VideoPauseWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('oe_video_pause_work');
        // 设置参数
        $this->setDescription('巨量无效素材关停');

        // 设置参数
        $this->setHelp('php think oe_video_pause_work');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        $this->getOutputMessage('巨量无效素材关停开始');

        $arr = [];

        foreach ($arr as $v) {
            $advertiserId = $v[0];
            $materialId = $v[1];

            $token = AccountService::getToken(CommonEnum::OE_MEDIA, $advertiserId);
            if (! $token) {
                return self::setError('token获取失败');
            }
            $headers = [
                'Access-Token' => $token,
                'Content-Type' => 'application/json',
            ];
            $params = [
                'advertiser_id' => $advertiserId,
                'material_ids' => [$materialId],
            ];

            $res = MaterialService::setVideoPause($headers, $params);

            if (! $res) {
                $this->getOutputMessage('巨量无效素材[' . $v['0'] . '-' . $v[1] . ']关停失败:' . VideoService::getError(0));
            } else {
                $this->getOutputMessage('巨量无效素材[' . $v['0'] . '-' . $v[1] . ']关停成功:' . json_encode($res));
            }
        }

        /*
        $st = time() + 5 * 60;
        (new OeRebateMaterialModel())
            ->where('status', '=', OeRebateMaterialModel::STATUS_STOP_YES)
            ->chunk(100, function ($items) use ($st) {
                if (! $items) {
                    return false;
                }
                if ($st <= time()) {
                    return false;
                }
                foreach ($items as $item) {
                    if (! $item instanceof OeRebateMaterialModel) {
                        continue;
                    }
                    $id = $item->getKey();
                    $this->getOutputMessage('巨量无效素材[' . $id . ']关停开始');
                    if (! VideoService::VideoPause($item)) {
                        $this->getOutputMessage('巨量无效素材[' . $id . ']关停失败:' . VideoService::getError(0));
                    }
                    $this->getOutputMessage('巨量无效素材[' . $id . ']关停结束');
                }
            }, 'id', 'desc');
        */
        $this->getOutputMessage('巨量无效素材关停结束');
    }
}
